package com.darren.euc.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 帐单
 * Created by IIS72013 2018/3/22.
 */
@Entity
@Table(name = "euc_bills")
public class Bill extends BaseEntity implements Serializable {

	private static final long serialVersionUID = 8966198794285355284L;

	@Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "bill_code",length = 20)
    private String billCode;

    @Column(name = "space_id")
    private Integer spaceId;

    @Column(name = "space_name")
    private String spaceName;

    @Column(name = "client_id")
    private Integer clientId;
    
    @Column(name = "client_name")
    private String clientName;

    @Column(name = "payable_amount",columnDefinition = "Decimal(13,2) default '0' comment '应收总额'")
    private Double payableAmount;//应付款总额

    @Column(name = "paid_amount",columnDefinition = "Decimal(13,2) default '0' comment '回款金额'")
    private Double paidAmount;//回款金额   
    
    
    @Column(name = "bill_status",columnDefinition = "bigint  comment '帐单状态'")
    private Integer billStatus;//帐单状态：1 未结清;2 已结清;3 已逾期
    
    
    @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
    @Column(name = "bill_date")//帐单日
    private Date billDate;   
    
    @OneToMany(mappedBy = "bill", cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
	@OrderBy("createTime ASC")
    List<BillDetail> billDetails = new ArrayList<BillDetail>();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getBillCode() {
		return billCode;
	}

	public void setBillCode(String billCode) {
		this.billCode = billCode;
	}

	public Integer getSpaceId() {
		return spaceId;
	}

	public void setSpaceId(Integer spaceId) {
		this.spaceId = spaceId;
	}

	public String getSpaceName() {
		return spaceName;
	}

	public void setSpaceName(String spaceName) {
		this.spaceName = spaceName;
	}

	public Integer getClientId() {
		return clientId;
	}

	public void setClientId(Integer clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public Double getPayableAmount() {
		return payableAmount;
	}

	public void setPayableAmount(Double payableAmount) {
		this.payableAmount = payableAmount;
	}

	public Double getPaidAmount() {
		paidAmount = 0D;
		for(BillDetail billDetail:billDetails){
			paidAmount += billDetail.getPaidAmount();
		}
		return paidAmount;
	}

	public void setPaidAmount(Double paidAmount) {
		this.paidAmount = paidAmount;
	}

	public Integer getBillStatus() {
		billStatus = 2;
		for(BillDetail billDetail:billDetails){
			if(billDetail.getPaidAmount().compareTo(billDetail.getPayableAmount()) < 0){
				billStatus = 1;
			}
		}
		if(billStatus == 1){
			Date date=new   Date();//取时间
		    Calendar   calendar   =   new   GregorianCalendar(); 
		    calendar.setTime(billDate); 
		    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));  
		    calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 		
			if(date.getTime() < (new Date()).getTime()){
				billStatus = 3;
			}
		}
		return billStatus;
	}

	public void setBillStatus(Integer billStatus) {
		this.billStatus = billStatus;
	}

	public Date getBillDate() {
		return billDate;
	}

	public void setBillDate(Date billDate) {
		this.billDate = billDate;
	}

	public List<BillDetail> getBillDetails() {
		return billDetails;
	}

	public void setBillDetails(List<BillDetail> billDetails) {
		this.billDetails = billDetails;
	}
    
}
